﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;

namespace Kmust.Commom
{
    public class OrganizationNodeItem : OrganizationBase, INotifyPropertyChanged
    {
        /// <summary>
        /// 选中状态
        /// </summary>//
        private bool _isChecked;
        private OrganizationNodeItem parentNodeItem;

        public OrganizationNodeItem ParentNodeItem
        {
            get { return parentNodeItem; }
            set {
                if (parentNodeItem != value)
                {
                    parentNodeItem = value;
                    NotifyPropertyChanged("ParentNodeItem");
                }
            }
        }

        public bool IsChecked
        {
            get { return this._isChecked; }
            set {
                this._isChecked = value;
                ChangeChildNodes(this);
                ChangedParentNode(this);
                NotifyPropertyChanged("IsChecked");
            }
        }
        /// <summary>
        /// 向下遍历,更改孩子节点状态
        /// 注意：这里的父节点不是属性而是字段
        /// 采用字段的原因是因为不想让父节点触发访问器而触发Setter
        /// </summary>
        /// <param name="CurrentNode"></param>
        public void ChangeChildNodes(OrganizationNodeItem CurrentNode)
        {
            if (CurrentNode.Children != null)
            {
                foreach (var data in CurrentNode.Children)
                {
                    data._isChecked = CurrentNode._isChecked;
                    data.NotifyPropertyChanged("IsChecked");
                    if (data.Children != null)
                    {
                        data.ChangeChildNodes(data);
                    }
                }
            }
        }

        /// <summary>
        /// 向上遍历,更改父节点状态
        /// 注意：这里的父节点不是属性而是字段
        /// 采用字段的原因是因为不想让父节点触发访问器而触发Setter
        /// </summary>
        /// <param name="CurrentNode"></param>
        public void ChangedParentNode(OrganizationNodeItem CurrentNode)
        {
            if (CurrentNode.ParentNodeItem != null)
            {
                bool isCheck = true;
                foreach (var data in CurrentNode.ParentNodeItem.Children)
                {
                    if (data.IsChecked != true)
                    {
                        isCheck = false;
                        break;
                    }
                }
                CurrentNode.ParentNodeItem.IsChecked = isCheck;
                CurrentNode.ParentNodeItem.NotifyPropertyChanged("IsChecked");
            }
        }


        ///// <summary>
        ///// 向上遍历,更改父节点状态
        ///// 注意：这里的父节点不是属性而是字段
        ///// 采用字段的原因是因为不想让父节点触发访问器而触发Setter
        ///// </summary>
        ///// <param name="CurrentNode"></param>
        //public void ChangedParentNodes(Device CurrentNode)
        //{
        //    if (CurrentNode.ParentNode != null)
        //    {
        //        bool? parentNodeState = true;
        //        int selectedCount = 0;  //被选中的个数
        //        int noSelectedCount = 0;    //不被选中的个数

        //        foreach (var data in CurrentNode.ParentNode.ChildNodes)
        //        {
        //            if (data.IsSelected == true)
        //            {
        //                selectedCount++;
        //            }
        //            else if (data.IsSelected == false)
        //            {
        //                noSelectedCount++;
        //            }
        //        }

        //        //如果全部被选中,则修改父节点为选中
        //        if (selectedCount ==
        //            CurrentNode.ParentNode.ChildNodes.Count)
        //        {
        //            parentNodeState = true;
        //        }
        //        //如果全部不被选中,则修改父节点为不被选中
        //        else if (noSelectedCount ==
        //            CurrentNode.ParentNode.ChildNodes.Count)
        //        {
        //            parentNodeState = false;
        //        }
        //        //否则标记父节点（例如用实体矩形填满）
        //        else
        //        {
        //            parentNodeState = null;
        //        }

        //        CurrentNode.parentNode.isSelected = parentNodeState;
        //        CurrentNode.parentNode.NotifyPropertyChanged("IsSelected");

        //        if (CurrentNode.ParentNode.ParentNode != null)
        //        {
        //            ChangedParentNodes(CurrentNode.parentNode);
        //        }
        //    }
        //}
      //  public string Code { set; get; }
        public List<OrganizationNodeItem> Children { set; get; }
        public bool Expand { set; get; }
        public OrganizationNodeItem()
        {

            Children = new List<OrganizationNodeItem>();
        }

        /// <summary>
        /// 展开树节点到某一级 递归算法                                                                                                      /// <param name="list">构建树的所有节点集合</param>
        /// <param name="lev"></param>
        private void ExpandInternal(List<OrganizationNodeItem> list, int lev)
        {
            //if (lev > 3)  //展开到第三层
            //    return;
            list.ForEach(s => ExpandInternal(s.Children, lev + 1));
            list.ForEach(s => s.Expand = true);
        }
        public void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        public event PropertyChangedEventHandler PropertyChanged;
       
    }
}
